package ferp.core.ai.filter;

import ferp.core.card.Card;
import ferp.core.player.Hand;

import java.util.Arrays;

/**
 * User: olegs
 * Date: 1/23/13 10:47 AM
 */
public class FilterThridQueenTrick
{
  // the method is called for player that is not declarer, has hand 0 and for first 4 rounds only
  public static int apply(int candidates, int declarerCards)
  {
    // clear the third queen suits array
    Arrays.fill(suits, null);
    int size = 0;

    // determine if declarer has third queen
    for (Card.Suit s : Card.Suit.rising)
      if (Hand.only(candidates, s) != 0)
      {
        int cards = Hand.get(declarerCards, s);
        int length = Card.Suit.length(cards);

        if (!Hand.contains(cards, Card.Mask.ACE) && !Hand.contains(cards, Card.Mask.KING) && Hand.contains(cards, Card.Mask.QUEEN) && length == 3)
          suits[size++] = s;
      }

    // if there is no third queen in any suit - return all candidates
    if (size == 0)
      return candidates;

    int filtered = candidates;

    // all remaining suit sets should be removed from candidates as they can make the third queen to take a trick
    for (int i = 0; i < size; ++i)
    {
      Card.Suit s = suits[i];

      if (s != null)
        filtered = Hand.remove(filtered, Hand.only(filtered, s));
    }

    if (filtered == 0)
      return candidates;

    return filtered;
  }

  // array containing third queen suits
  private final static Card.Suit[] suits = new Card.Suit[Card.Suit.rising.length];
}
